// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// v [author] rhyme_lph
// J [email]  rhymelph@gmail.com
// h [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/provider/setting/setting_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/reg_exp_utils.dart';
import 'package:finance_app/src/websocket/web_socket_widget.dart';
import 'package:finance_app/src/widgets/button/assistant_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/list_item/setting_item.dart';
import 'package:flutter/material.dart';
import 'package:quiver/strings.dart';
import 'package:url_launcher/url_launcher.dart';

@RouterMeta(paramName: "settingPage")
class SettingPage extends StatefulWidget {
  @override
  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> with PagePresenter<SettingPage, SettingProvider> {
  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.settings,
        actions: [
          AssistantButton(
            entryPoint: "Settings",
          ),
        ],
      );

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            AppSpaces.h20,
            SearchInputField(
              controller: provider.searchController,
              focusNode: provider.searchFocusNode,
              tip: S.current.search,
            ),
            AppSpaces.h20,
            listBuilder<bool>(
                builder: (_, __, ___) => __ == true
                    ? listBuilder<int>(
                        builder: (_, __, ___) => Column(
                              children: provider.functionList
                                  .map(
                                    (e) => SettingItem(
                                      title: e['title'],
                                      content: e['content'],
                                      svgPath: e['path'],
                                      onTap: () => provider.onNavFunction(e['router']),
                                    ),
                                  )
                                  .toList(),
                            ),
                        selector: (_, __) => provider.functionList.length)
                    : listBuilder<String>(
                        builder: (BuildContext context, String value, Widget child) => Column(
                          children: provider.functionList
                              .where((element) => RegExpUtils.getSearchRegExp(value).hasMatch(element['title']))
                              .map(
                                (e) => SettingItem(
                                  title: e['title'],
                                  content: e['content'],
                                  svgPath: e['path'],
                                  onTap: () => provider.onNavFunction(e['router']),
                                ),
                              )
                              .toList(),
                        ),
                        selector: (_, __) => __.searchController.text,
                      ),
                selector: (_, __) => isEmpty(__.searchController.text)),
            AppSpaces.h25,
            DefaultTextStyle(
              style: TextStyle(
                color: aceBlackColor,
                fontSize: getSp(14),
                fontWeight: FontWeight.w700,
                height: 14 / 22,
                fontFamily: "DinRoundPro",
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  PressedOpacityButton(
                    onPressed: () {
                      launch(Config.termOfServiceUrl);
                    },
                    child: Text(
                      S.current.terms_of_service,
                    ),
                  ),
                  Container(
                    width: getDp(3),
                    height: getDp(3),
                    margin: EdgeInsets.symmetric(horizontal: getDp(10)),
                    decoration: BoxDecoration(color: aceGrayColor, shape: BoxShape.circle),
                  ),
                  PressedOpacityButton(
                    onPressed: () {
                      launch(Config.privacyPolicyUrl);
                    },
                    child: Text(
                      S.current.privacy_policy,
                    ),
                  ),
                ],
              ),
            ),
            AppSpaces.h40,
          ],
        ),
      );
}
